package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.SavaVehicleParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {


    @Autowired
    VehicleMapper vehicleMapper;

    public VehicleServiceImpl(@Qualifier("vehicleMapper") VehicleMapper vehicleMapper) {
        this.vehicleMapper = vehicleMapper;
    }

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询用户业务:{}",vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;
    }

    @Override
    public void saveVehicle(SavaVehicleParam savaVehicleQuery) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(savaVehicleQuery,vehicle);


        if(vehicle.getId() == null){
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.saveVehicle(vehicle);
        }else{
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        vehicleMapper.deleteVehicle(id);
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("解绑车辆业务:{}",vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geoId, Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(geoId);
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public List<VehicleVO> selectDistribute(VehicleQuery vehicleQuery) {
        log.debug("查询 用户业务:{}",vehicleQuery);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleQuery,vehicle);
        vehicle.setStatus("1");
        List<VehicleVO> list = vehicleMapper.selectDistribute(vehicle);
        return list;
    }

}
